package org.wxd.media.domain;

import com.mongodb.gridfs.GridFSFile;

import java.util.Map;

/**
 * Created by wangxd on 2016/11/2.
 */
public final class GridFsHelper {


    public static Media asEntity(GridFSFile gridFile){
        if (gridFile == null) return null;
        Map metaData = gridFile.getMetaData().toMap();
        metaData.remove(Media.CLASS);
        long createTime = gridFile.getUploadDate().getTime();
        long expireTime = (long) metaData.remove(Media.EXPIRE_TIME_META_NAME);
        return Media.newBuilder()
                .id(gridFile.getId().toString()).name(gridFile.getFilename())
                .contentType(gridFile.getContentType()).checksum(gridFile.getMD5())
                .moreInfo((Map<String, String>) metaData.get(Media.MORE_INFO))
                .suffix((String) metaData.get(Media.SUFFIX))
                .size(gridFile.getLength())
                .createTime(createTime)
                .thumbnail((Boolean) metaData.get(Media.THUMBNAIL))
                .expireTime(expireTime - createTime)
                .build();
    }
}
